<html>
  <head>
  <title>util.py</title>
  </head>
  <body>
  <h3>util.py (<a href="../util.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># util.py
# -------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>sys
<span style="color: blue; font-weight: bold">import </span>inspect
<span style="color: blue; font-weight: bold">import </span>heapq<span style="font-weight: bold">, </span>random


<span style="color: darkred">"""
 Data structures useful for implementing SearchAgents
"""

</span><span style="color: blue; font-weight: bold">class </span>Stack<span style="font-weight: bold">:
  </span><span style="color: red">"A container with a last-in-first-out (LIFO) queuing policy."
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list <span style="font-weight: bold">= []
    
  </span><span style="color: blue; font-weight: bold">def </span>push<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">,</span>item<span style="font-weight: bold">):
    </span><span style="color: red">"Push 'item' onto the stack"
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>item<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>pop<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: red">"Pop the most recently pushed item from the stack"
    </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list<span style="font-weight: bold">.</span>pop<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>isEmpty<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: red">"Returns true if the stack is empty"
    </span><span style="color: blue; font-weight: bold">return </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list<span style="font-weight: bold">) == </span><span style="color: red">0

</span><span style="color: blue; font-weight: bold">class </span>Queue<span style="font-weight: bold">:
  </span><span style="color: red">"A container with a first-in-first-out (FIFO) queuing policy."
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list <span style="font-weight: bold">= []
  
  </span><span style="color: blue; font-weight: bold">def </span>push<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">,</span>item<span style="font-weight: bold">):
    </span><span style="color: red">"Enqueue the 'item' into the queue"
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span>item<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>pop<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
      Dequeue the earliest enqueued item still in the queue. This
      operation removes the item from the queue.
    """
    </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list<span style="font-weight: bold">.</span>pop<span style="font-weight: bold">()

  </span><span style="color: blue; font-weight: bold">def </span>isEmpty<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: red">"Returns true if the queue is empty"
    </span><span style="color: blue; font-weight: bold">return </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>list<span style="font-weight: bold">) == </span><span style="color: red">0
  
</span><span style="color: blue; font-weight: bold">class </span>PriorityQueue<span style="font-weight: bold">:
  </span><span style="color: darkred">"""
    Implements a priority queue data structure. Each inserted item
    has a priority associated with it and the client is usually interested
    in quick retrieval of the lowest-priority item in the queue. This
    data structure allows O(1) access to the lowest-priority item.
    
    Note that this PriorityQueue does not allow you to change the priority
    of an item.  However, you may insert the same item multiple times with
    different priorities.
  """  
  </span><span style="color: blue; font-weight: bold">def  </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):  
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>heap <span style="font-weight: bold">= []
    
  </span><span style="color: blue; font-weight: bold">def </span>push<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>item<span style="font-weight: bold">, </span>priority<span style="font-weight: bold">):
      </span>pair <span style="font-weight: bold">= (</span>priority<span style="font-weight: bold">,</span>item<span style="font-weight: bold">)
      </span>heapq<span style="font-weight: bold">.</span>heappush<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>heap<span style="font-weight: bold">,</span>pair<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>pop<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
      (</span>priority<span style="font-weight: bold">,</span>item<span style="font-weight: bold">) = </span>heapq<span style="font-weight: bold">.</span>heappop<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>heap<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">return </span>item
  
  <span style="color: blue; font-weight: bold">def </span>isEmpty<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">return </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>heap<span style="font-weight: bold">) == </span><span style="color: red">0

</span><span style="color: blue; font-weight: bold">class </span>PriorityQueueWithFunction<span style="font-weight: bold">(</span>PriorityQueue<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  Implements a priority queue with the same push/pop signature of the
  Queue and the Stack classes. This is designed for drop-in replacement for
  those two classes. The caller has to provide a priority function, which
  extracts each item's priority.
  """  
  </span><span style="color: blue; font-weight: bold">def  </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>priorityFunction<span style="font-weight: bold">):
    </span><span style="color: red">"priorityFunction (item) -&gt; priority"
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>priorityFunction <span style="font-weight: bold">= </span>priorityFunction      <span style="color: green; font-style: italic"># store the priority function
    </span>PriorityQueue<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)        </span><span style="color: green; font-style: italic"># super-class initializer
    
  </span><span style="color: blue; font-weight: bold">def </span>push<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>item<span style="font-weight: bold">):
    </span><span style="color: red">"Adds an item to the queue with priority from the priority function"
    </span>PriorityQueue<span style="font-weight: bold">.</span>push<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>item<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>priorityFunction<span style="font-weight: bold">(</span>item<span style="font-weight: bold">))

    
</span><span style="color: blue; font-weight: bold">def </span>manhattanDistance<span style="font-weight: bold">( </span>xy1<span style="font-weight: bold">, </span>xy2 <span style="font-weight: bold">):
  </span><span style="color: red">"Returns the Manhattan distance between points xy1 and xy2"
  </span><span style="color: blue; font-weight: bold">return </span>abs<span style="font-weight: bold">( </span>xy1<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] - </span>xy2<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] ) + </span>abs<span style="font-weight: bold">( </span>xy1<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] - </span>xy2<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] )

</span><span style="color: darkred">"""
  Data structures and functions useful for various course projects
  
  The search project should not need anything below this line.
"""

</span><span style="color: blue; font-weight: bold">class </span>Counter<span style="font-weight: bold">(</span>dict<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  A counter keeps track of counts for a set of keys.
  
  The counter class is an extension of the standard python
  dictionary type.  It is specialized to have number values  
  (integers or floats), and includes a handful of additional
  functions to ease the task of counting data.  In particular, 
  all keys are defaulted to have value 0.  Using a dictionary:
  
  a = {}
  print a['test']
  
  would give an error, while the Counter class analogue:
    
  &gt;&gt;&gt; a = Counter()
  &gt;&gt;&gt; print a['test']
  0

  returns the default 0 value. Note that to reference a key 
  that you know is contained in the counter, 
  you can still use the dictionary syntax:
    
  &gt;&gt;&gt; a = Counter()
  &gt;&gt;&gt; a['test'] = 2
  &gt;&gt;&gt; print a['test']
  2
  
  This is very useful for counting things without initializing their counts,
  see for example:
  
  &gt;&gt;&gt; a['blah'] += 1
  &gt;&gt;&gt; print a['blah']
  1
  
  The counter also includes additional functionality useful in implementing
  the classifiers for this assignment.  Two counters can be added,
  subtracted or multiplied together.  See below for details.  They can
  also be normalized and their total count and arg max can be extracted.
  """
  </span><span style="color: blue; font-weight: bold">def </span>__getitem__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>idx<span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>setdefault<span style="font-weight: bold">(</span>idx<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>dict<span style="font-weight: bold">.</span>__getitem__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>idx<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>incrementAll<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>keys<span style="font-weight: bold">, </span>count<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Increments all elements of keys by the same count.
    
    &gt;&gt;&gt; a = Counter()
    &gt;&gt;&gt; a.incrementAll(['one','two', 'three'], 1)
    &gt;&gt;&gt; a['one']
    1
    &gt;&gt;&gt; a['two']
    1
    """
    </span><span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span>keys<span style="font-weight: bold">:
      </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] += </span>count
  
  <span style="color: blue; font-weight: bold">def </span>argMax<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the key with the highest value.
    """
    </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>keys<span style="font-weight: bold">()) == </span><span style="color: red">0</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">None
    </span>all <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>items<span style="font-weight: bold">()
    </span>values <span style="font-weight: bold">= [</span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>all<span style="font-weight: bold">]
    </span>maxIndex <span style="font-weight: bold">= </span>values<span style="font-weight: bold">.</span>index<span style="font-weight: bold">(</span>max<span style="font-weight: bold">(</span>values<span style="font-weight: bold">))
    </span><span style="color: blue; font-weight: bold">return </span>all<span style="font-weight: bold">[</span>maxIndex<span style="font-weight: bold">][</span><span style="color: red">0</span><span style="font-weight: bold">]
  
  </span><span style="color: blue; font-weight: bold">def </span>sortedKeys<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns a list of keys sorted by their values.  Keys
    with the highest values will appear first.
    
    &gt;&gt;&gt; a = Counter()
    &gt;&gt;&gt; a['first'] = -2
    &gt;&gt;&gt; a['second'] = 4
    &gt;&gt;&gt; a['third'] = 1
    &gt;&gt;&gt; a.sortedKeys()
    ['second', 'third', 'first']
    """
    </span>sortedItems <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>items<span style="font-weight: bold">()
    </span>compare <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">lambda </span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">:  </span>sign<span style="font-weight: bold">(</span>y<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] - </span>x<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">])
    </span>sortedItems<span style="font-weight: bold">.</span>sort<span style="font-weight: bold">(</span>cmp<span style="font-weight: bold">=</span>compare<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>x<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>sortedItems<span style="font-weight: bold">]
  
  </span><span style="color: blue; font-weight: bold">def </span>totalCount<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns the sum of counts for all keys.
    """
    </span><span style="color: blue; font-weight: bold">return </span>sum<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>values<span style="font-weight: bold">())
  
  </span><span style="color: blue; font-weight: bold">def </span>normalize<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Edits the counter such that the total count of all
    keys sums to 1.  The ratio of counts for all keys
    will remain the same. Note that normalizing an empty 
    Counter will result in an error.
    """
    </span>total <span style="font-weight: bold">= </span>float<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>totalCount<span style="font-weight: bold">())
    </span><span style="color: blue; font-weight: bold">if </span>total <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return
    for </span>key <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>keys<span style="font-weight: bold">():
      </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] / </span>total
      
  <span style="color: blue; font-weight: bold">def </span>divideAll<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>divisor<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Divides all counts by divisor
    """
    </span>divisor <span style="font-weight: bold">= </span>float<span style="font-weight: bold">(</span>divisor<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">:
      </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] /= </span>divisor

  <span style="color: blue; font-weight: bold">def </span>copy<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Returns a copy of the counter
    """
    </span><span style="color: blue; font-weight: bold">return </span>Counter<span style="font-weight: bold">(</span>dict<span style="font-weight: bold">.</span>copy<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">))
  
  </span><span style="color: blue; font-weight: bold">def </span>__mul__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>y <span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Multiplying two counters gives the dot product of their vectors where
    each unique label is a vector element.
    
    &gt;&gt;&gt; a = Counter()
    &gt;&gt;&gt; b = Counter()
    &gt;&gt;&gt; a['first'] = -2
    &gt;&gt;&gt; a['second'] = 4
    &gt;&gt;&gt; b['first'] = 3
    &gt;&gt;&gt; b['second'] = 5
    &gt;&gt;&gt; a['third'] = 1.5
    &gt;&gt;&gt; a['fourth'] = 2.5
    &gt;&gt;&gt; a * b
    14
    """
    </span>sum <span style="font-weight: bold">= </span><span style="color: red">0
    </span>x <span style="font-weight: bold">= </span><span style="color: blue">self
    </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span>x<span style="font-weight: bold">) &gt; </span>len<span style="font-weight: bold">(</span>y<span style="font-weight: bold">):
      </span>x<span style="font-weight: bold">,</span>y <span style="font-weight: bold">= </span>y<span style="font-weight: bold">,</span>x
    <span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span>x<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">if </span>key <span style="color: blue; font-weight: bold">not in </span>y<span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">continue
      </span>sum <span style="font-weight: bold">+= </span>x<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] * </span>y<span style="font-weight: bold">[</span>key<span style="font-weight: bold">]      
    </span><span style="color: blue; font-weight: bold">return </span>sum
      
  <span style="color: blue; font-weight: bold">def </span>__radd__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>y<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Adding another counter to a counter increments the current counter
    by the values stored in the second counter.
    
    &gt;&gt;&gt; a = Counter()
    &gt;&gt;&gt; b = Counter()
    &gt;&gt;&gt; a['first'] = -2
    &gt;&gt;&gt; a['second'] = 4
    &gt;&gt;&gt; b['first'] = 3
    &gt;&gt;&gt; b['third'] = 1
    &gt;&gt;&gt; a += b
    &gt;&gt;&gt; a['first']
    1
    """ 
    </span><span style="color: blue; font-weight: bold">for </span>key<span style="font-weight: bold">, </span>value <span style="color: blue; font-weight: bold">in </span>y<span style="font-weight: bold">.</span>items<span style="font-weight: bold">():
      </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] += </span>value   
      
  <span style="color: blue; font-weight: bold">def </span>__add__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>y <span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Adding two counters gives a counter with the union of all keys and
    counts of the second added to counts of the first.
    
    &gt;&gt;&gt; a = Counter()
    &gt;&gt;&gt; b = Counter()
    &gt;&gt;&gt; a['first'] = -2
    &gt;&gt;&gt; a['second'] = 4
    &gt;&gt;&gt; b['first'] = 3
    &gt;&gt;&gt; b['third'] = 1
    &gt;&gt;&gt; (a + b)['first']
    1
    """
    </span>addend <span style="font-weight: bold">= </span>Counter<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">if </span>key <span style="color: blue; font-weight: bold">in </span>y<span style="font-weight: bold">:
        </span>addend<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] + </span>y<span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>addend<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span>y<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">if </span>key <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">continue
      </span>addend<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span>y<span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">return </span>addend
    
  <span style="color: blue; font-weight: bold">def </span>__sub__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>y <span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    Subtracting a counter from another gives a counter with the union of all keys and
    counts of the second subtracted from counts of the first.
    
    &gt;&gt;&gt; a = Counter()
    &gt;&gt;&gt; b = Counter()
    &gt;&gt;&gt; a['first'] = -2
    &gt;&gt;&gt; a['second'] = 4
    &gt;&gt;&gt; b['first'] = 3
    &gt;&gt;&gt; b['third'] = 1
    &gt;&gt;&gt; (a - b)['first']
    -5
    """      
    </span>addend <span style="font-weight: bold">= </span>Counter<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">if </span>key <span style="color: blue; font-weight: bold">in </span>y<span style="font-weight: bold">:
        </span>addend<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">] - </span>y<span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>addend<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span><span style="color: blue">self</span><span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span>y<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">if </span>key <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">continue
      </span>addend<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = -</span><span style="color: red">1 </span><span style="font-weight: bold">* </span>y<span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">return </span>addend
    
<span style="color: blue; font-weight: bold">def </span>raiseNotDefined<span style="font-weight: bold">():
  </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">"Method not implemented: %s" </span><span style="font-weight: bold">% </span>inspect<span style="font-weight: bold">.</span>stack<span style="font-weight: bold">()[</span><span style="color: red">1</span><span style="font-weight: bold">][</span><span style="color: red">3</span><span style="font-weight: bold">]    
  </span>sys<span style="font-weight: bold">.</span>exit<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>normalize<span style="font-weight: bold">(</span>vectorOrCounter<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  normalize a vector or counter by dividing each value by the sum of all values
  """
  </span>normalizedCounter <span style="font-weight: bold">= </span>Counter<span style="font-weight: bold">()
  </span><span style="color: blue; font-weight: bold">if </span>type<span style="font-weight: bold">(</span>vectorOrCounter<span style="font-weight: bold">) == </span>type<span style="font-weight: bold">(</span>normalizedCounter<span style="font-weight: bold">):
    </span>counter <span style="font-weight: bold">= </span>vectorOrCounter
    total <span style="font-weight: bold">= </span>float<span style="font-weight: bold">(</span>counter<span style="font-weight: bold">.</span>totalCount<span style="font-weight: bold">())
    </span><span style="color: blue; font-weight: bold">if </span>total <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span>counter
    <span style="color: blue; font-weight: bold">for </span>key <span style="color: blue; font-weight: bold">in </span>counter<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">():
      </span>value <span style="font-weight: bold">= </span>counter<span style="font-weight: bold">[</span>key<span style="font-weight: bold">]
      </span>normalizedCounter<span style="font-weight: bold">[</span>key<span style="font-weight: bold">] = </span>value <span style="font-weight: bold">/ </span>total
    <span style="color: blue; font-weight: bold">return </span>normalizedCounter
  <span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
    </span>vector <span style="font-weight: bold">= </span>vectorOrCounter
    s <span style="font-weight: bold">= </span>float<span style="font-weight: bold">(</span>sum<span style="font-weight: bold">(</span>vector<span style="font-weight: bold">))
    </span><span style="color: blue; font-weight: bold">if </span>s <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span>vector
    <span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>el <span style="font-weight: bold">/ </span>s <span style="color: blue; font-weight: bold">for </span>el <span style="color: blue; font-weight: bold">in </span>vector<span style="font-weight: bold">]
                
</span><span style="color: blue; font-weight: bold">def </span>nSample<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">, </span>values<span style="font-weight: bold">, </span>n<span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">if </span>sum<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">) != </span><span style="color: red">1</span><span style="font-weight: bold">:
    </span>distribution <span style="font-weight: bold">= </span>normalize<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">)
  </span>rand <span style="font-weight: bold">= [</span>random<span style="font-weight: bold">.</span>random<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>n<span style="font-weight: bold">)]
  </span>rand<span style="font-weight: bold">.</span>sort<span style="font-weight: bold">()
  </span>samples <span style="font-weight: bold">= []
  </span>samplePos<span style="font-weight: bold">, </span>distPos<span style="font-weight: bold">, </span>cdf <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">, </span>distribution<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">while </span>samplePos <span style="font-weight: bold">&lt; </span>n<span style="font-weight: bold">:
    </span><span style="color: blue; font-weight: bold">if </span>rand<span style="font-weight: bold">[</span>samplePos<span style="font-weight: bold">] &lt; </span>cdf<span style="font-weight: bold">:
      </span>samplePos <span style="font-weight: bold">+= </span><span style="color: red">1
      </span>samples<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>values<span style="font-weight: bold">[</span>distPos<span style="font-weight: bold">])
    </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
      </span>distPos <span style="font-weight: bold">+= </span><span style="color: red">1
      </span>cdf <span style="font-weight: bold">+= </span>distribution<span style="font-weight: bold">[</span>distPos<span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">return </span>samples
    
<span style="color: blue; font-weight: bold">def </span>sample<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">, </span>values <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">if </span>type<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">) == </span>Counter<span style="font-weight: bold">: 
    </span>items <span style="font-weight: bold">= </span>distribution<span style="font-weight: bold">.</span>items<span style="font-weight: bold">()
    </span>distribution <span style="font-weight: bold">= [</span>i<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>items<span style="font-weight: bold">] 
    </span>values <span style="font-weight: bold">= [</span>i<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>items<span style="font-weight: bold">] 
  </span><span style="color: blue; font-weight: bold">if </span>sum<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">) != </span><span style="color: red">1</span><span style="font-weight: bold">:
    </span>distribution <span style="font-weight: bold">= </span>normalize<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">)
  </span>choice <span style="font-weight: bold">= </span>random<span style="font-weight: bold">.</span>random<span style="font-weight: bold">()
  </span>i<span style="font-weight: bold">, </span>total<span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">, </span>distribution<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">while </span>choice <span style="font-weight: bold">&gt; </span>total<span style="font-weight: bold">:
    </span>i <span style="font-weight: bold">+= </span><span style="color: red">1
    </span>total <span style="font-weight: bold">+= </span>distribution<span style="font-weight: bold">[</span>i<span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">return </span>values<span style="font-weight: bold">[</span>i<span style="font-weight: bold">]

</span><span style="color: blue; font-weight: bold">def </span>sampleFromCounter<span style="font-weight: bold">(</span>ctr<span style="font-weight: bold">):
  </span>items <span style="font-weight: bold">= </span>ctr<span style="font-weight: bold">.</span>items<span style="font-weight: bold">()
  </span><span style="color: blue; font-weight: bold">return </span>sample<span style="font-weight: bold">([</span>v <span style="color: blue; font-weight: bold">for </span>k<span style="font-weight: bold">,</span>v <span style="color: blue; font-weight: bold">in </span>items<span style="font-weight: bold">], [</span>k <span style="color: blue; font-weight: bold">for </span>k<span style="font-weight: bold">,</span>v <span style="color: blue; font-weight: bold">in </span>items<span style="font-weight: bold">])

</span><span style="color: blue; font-weight: bold">def </span>getProbability<span style="font-weight: bold">(</span>value<span style="font-weight: bold">, </span>distribution<span style="font-weight: bold">, </span>values<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
    Gives the probability of a value under a discrete distribution
    defined by (distributions, values).
  """
  </span>total <span style="font-weight: bold">= </span><span style="color: red">0.0
  </span><span style="color: blue; font-weight: bold">for </span>prob<span style="font-weight: bold">, </span>val <span style="color: blue; font-weight: bold">in </span>zip<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">, </span>values<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>val <span style="font-weight: bold">== </span>value<span style="font-weight: bold">:
      </span>total <span style="font-weight: bold">+= </span>prob
  <span style="color: blue; font-weight: bold">return </span>total

<span style="color: blue; font-weight: bold">def </span>flipCoin<span style="font-weight: bold">( </span>p <span style="font-weight: bold">):
  </span>r <span style="font-weight: bold">= </span>random<span style="font-weight: bold">.</span>random<span style="font-weight: bold">()
  </span><span style="color: blue; font-weight: bold">return </span>r <span style="font-weight: bold">&lt; </span>p 

<span style="color: blue; font-weight: bold">def </span>chooseFromDistribution<span style="font-weight: bold">( </span>distribution <span style="font-weight: bold">):
  </span><span style="color: red">"Takes either a counter or a list of (prob, key) pairs and samples"
  </span><span style="color: blue; font-weight: bold">if </span>type<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">) == </span>dict <span style="color: blue; font-weight: bold">or </span>type<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">) == </span>Counter<span style="font-weight: bold">:
    </span><span style="color: blue; font-weight: bold">return </span>sample<span style="font-weight: bold">(</span>distribution<span style="font-weight: bold">)
  </span>r <span style="font-weight: bold">= </span>random<span style="font-weight: bold">.</span>random<span style="font-weight: bold">()
  </span>base <span style="font-weight: bold">= </span><span style="color: red">0.0
  </span><span style="color: blue; font-weight: bold">for </span>prob<span style="font-weight: bold">, </span>element <span style="color: blue; font-weight: bold">in </span>distribution<span style="font-weight: bold">:
    </span>base <span style="font-weight: bold">+= </span>prob
    <span style="color: blue; font-weight: bold">if </span>r <span style="font-weight: bold">&lt;= </span>base<span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span>element
    
<span style="color: blue; font-weight: bold">def </span>nearestPoint<span style="font-weight: bold">( </span>pos <span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  Finds the nearest grid point to a position (discretizes).
  """
  </span><span style="font-weight: bold">( </span>current_row<span style="font-weight: bold">, </span>current_col <span style="font-weight: bold">) = </span>pos

  grid_row <span style="font-weight: bold">= </span>int<span style="font-weight: bold">( </span>current_row <span style="font-weight: bold">+ </span><span style="color: red">0.5 </span><span style="font-weight: bold">) 
  </span>grid_col <span style="font-weight: bold">= </span>int<span style="font-weight: bold">( </span>current_col <span style="font-weight: bold">+ </span><span style="color: red">0.5 </span><span style="font-weight: bold">) 
  </span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">( </span>grid_row<span style="font-weight: bold">, </span>grid_col <span style="font-weight: bold">)     

</span><span style="color: blue; font-weight: bold">def </span>sign<span style="font-weight: bold">( </span>x <span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  Returns 1 or -1 depending on the sign of x
  """
  </span><span style="color: blue; font-weight: bold">if</span><span style="font-weight: bold">( </span>x <span style="font-weight: bold">&gt;= </span><span style="color: red">0 </span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">return </span><span style="color: red">1
  </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
    </span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">-</span><span style="color: red">1

</span><span style="color: blue; font-weight: bold">def </span>arrayInvert<span style="font-weight: bold">(</span>array<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  Inverts a matrix stored as a list of lists.
  """
  </span>result <span style="font-weight: bold">= [[] </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>array<span style="font-weight: bold">]
  </span><span style="color: blue; font-weight: bold">for </span>outer <span style="color: blue; font-weight: bold">in </span>array<span style="font-weight: bold">:
    </span><span style="color: blue; font-weight: bold">for </span>inner <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>len<span style="font-weight: bold">(</span>outer<span style="font-weight: bold">)):
      </span>result<span style="font-weight: bold">[</span>inner<span style="font-weight: bold">].</span>append<span style="font-weight: bold">(</span>outer<span style="font-weight: bold">[</span>inner<span style="font-weight: bold">])
  </span><span style="color: blue; font-weight: bold">return </span>result

<span style="color: blue; font-weight: bold">def </span>matrixAsList<span style="font-weight: bold">( </span>matrix<span style="font-weight: bold">, </span>value <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True </span><span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  Turns a matrix into a list of coordinates matching the specified value
  """
  </span>rows<span style="font-weight: bold">, </span>cols <span style="font-weight: bold">= </span>len<span style="font-weight: bold">( </span>matrix <span style="font-weight: bold">), </span>len<span style="font-weight: bold">( </span>matrix<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] )
  </span>cells <span style="font-weight: bold">= []
  </span><span style="color: blue; font-weight: bold">for </span>row <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">( </span>rows <span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">for </span>col <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">( </span>cols <span style="font-weight: bold">):
      </span><span style="color: blue; font-weight: bold">if </span>matrix<span style="font-weight: bold">[</span>row<span style="font-weight: bold">][</span>col<span style="font-weight: bold">] == </span>value<span style="font-weight: bold">:
        </span>cells<span style="font-weight: bold">.</span>append<span style="font-weight: bold">( ( </span>row<span style="font-weight: bold">, </span>col <span style="font-weight: bold">) )
  </span><span style="color: blue; font-weight: bold">return </span>cells

<span style="color: blue; font-weight: bold">def </span>lookup<span style="font-weight: bold">(</span>name<span style="font-weight: bold">, </span>namespace<span style="font-weight: bold">):
  </span><span style="color: darkred">"""
  Get a method or class from any imported module from its name.
  Usage: lookup(functionName, globals())
  """
  </span>dots <span style="font-weight: bold">= </span>name<span style="font-weight: bold">.</span>count<span style="font-weight: bold">(</span><span style="color: red">'.'</span><span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>dots <span style="font-weight: bold">&gt; </span><span style="color: red">0</span><span style="font-weight: bold">:
    </span>moduleName<span style="font-weight: bold">, </span>objName <span style="font-weight: bold">= </span><span style="color: red">'.'</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>name<span style="font-weight: bold">.</span>split<span style="font-weight: bold">(</span><span style="color: red">'.'</span><span style="font-weight: bold">)[:-</span><span style="color: red">1</span><span style="font-weight: bold">]), </span>name<span style="font-weight: bold">.</span>split<span style="font-weight: bold">(</span><span style="color: red">'.'</span><span style="font-weight: bold">)[-</span><span style="color: red">1</span><span style="font-weight: bold">]
    </span>module <span style="font-weight: bold">= </span>__import__<span style="font-weight: bold">(</span>moduleName<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span>getattr<span style="font-weight: bold">(</span>module<span style="font-weight: bold">, </span>objName<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
    </span>modules <span style="font-weight: bold">= [</span>obj <span style="color: blue; font-weight: bold">for </span>obj <span style="color: blue; font-weight: bold">in </span>namespace<span style="font-weight: bold">.</span>values<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">if </span>str<span style="font-weight: bold">(</span>type<span style="font-weight: bold">(</span>obj<span style="font-weight: bold">)) == </span><span style="color: red">"&lt;type 'module'&gt;"</span><span style="font-weight: bold">]
    </span>options <span style="font-weight: bold">= [</span>getattr<span style="font-weight: bold">(</span>module<span style="font-weight: bold">, </span>name<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>module <span style="color: blue; font-weight: bold">in </span>modules <span style="color: blue; font-weight: bold">if </span>name <span style="color: blue; font-weight: bold">in </span>dir<span style="font-weight: bold">(</span>module<span style="font-weight: bold">)]
    </span>options <span style="font-weight: bold">+= [</span>obj<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>obj <span style="color: blue; font-weight: bold">in </span>namespace<span style="font-weight: bold">.</span>items<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">if </span>obj<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] == </span>name <span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span>options<span style="font-weight: bold">) == </span><span style="color: red">1</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">return </span>options<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]
    </span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span>options<span style="font-weight: bold">) &gt; </span><span style="color: red">1</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">raise </span>Exception<span style="font-weight: bold">, </span><span style="color: red">'Name conflict for %s'
    </span><span style="color: blue; font-weight: bold">raise </span>Exception<span style="font-weight: bold">, </span><span style="color: red">'%s not found as a method or class' </span><span style="font-weight: bold">% </span>name

<span style="color: blue; font-weight: bold">def </span>pause<span style="font-weight: bold">():
  </span><span style="color: darkred">"""
  Pauses the output stream awaiting user feedback.
  """
  </span><span style="color: blue; font-weight: bold">print </span><span style="color: red">"&lt;Press enter/return to continue&gt;"
  </span>raw_input<span style="font-weight: bold">()
  
  
</span><span style="color: green; font-style: italic">## code to handle timeouts
</span><span style="color: blue; font-weight: bold">import </span>signal
<span style="color: blue; font-weight: bold">class </span>TimeoutFunctionException<span style="font-weight: bold">(</span>Exception<span style="font-weight: bold">):
    </span><span style="color: darkred">"""Exception to raise on a timeout"""
    </span><span style="color: blue; font-weight: bold">pass

class </span>TimeoutFunction<span style="font-weight: bold">:

    </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>function<span style="font-weight: bold">, </span>timeout<span style="font-weight: bold">):
        </span><span style="color: red">"timeout must be at least 1 second. WHY??"
        </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>timeout <span style="font-weight: bold">= </span>timeout
        <span style="color: blue">self</span><span style="font-weight: bold">.</span>function <span style="font-weight: bold">= </span>function

    <span style="color: blue; font-weight: bold">def </span>handle_timeout<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>signum<span style="font-weight: bold">, </span>frame<span style="font-weight: bold">):
        </span><span style="color: blue; font-weight: bold">raise </span>TimeoutFunctionException<span style="font-weight: bold">()

    </span><span style="color: blue; font-weight: bold">def </span>__call__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, *</span>args<span style="font-weight: bold">):
        </span><span style="color: blue; font-weight: bold">if not </span><span style="color: red">'SIGALRM' </span><span style="color: blue; font-weight: bold">in </span>dir<span style="font-weight: bold">(</span>signal<span style="font-weight: bold">):
            </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>function<span style="font-weight: bold">(*</span>args<span style="font-weight: bold">)
        </span>old <span style="font-weight: bold">= </span>signal<span style="font-weight: bold">.</span>signal<span style="font-weight: bold">(</span>signal<span style="font-weight: bold">.</span>SIGALRM<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>handle_timeout<span style="font-weight: bold">)
        </span>signal<span style="font-weight: bold">.</span>alarm<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>timeout<span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">try</span><span style="font-weight: bold">:
            </span>result <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>function<span style="font-weight: bold">(*</span>args<span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">finally</span><span style="font-weight: bold">:
            </span>signal<span style="font-weight: bold">.</span>signal<span style="font-weight: bold">(</span>signal<span style="font-weight: bold">.</span>SIGALRM<span style="font-weight: bold">, </span>old<span style="font-weight: bold">)
        </span>signal<span style="font-weight: bold">.</span>alarm<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">return </span>result

  </pre>
  </body>
  </html>
  